package com.huluobo.a2109monthdemo.detail

import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.huluobo.a2109monthdemo.base.BaseViewModel
import com.huluobo.a2109monthdemo.net.Const
import okhttp3.MediaType
import okhttp3.RequestBody

/**
 *  Created by LC on 2024/1/26.
 */
class DetailViewModel : BaseViewModel() {
    private val repo = DetailRepo()
    val getGoodsDetailSuccess = MutableLiveData<DetailBeanResult>()
    val getGoodsDetailFailed = MutableLiveData<String>()

    val addToCartSuccess = MutableLiveData<AddCartBeanResult>()
    val addToCartFailed = MutableLiveData<String>()

    fun getGoodsDetail(goodsId: Int) {
        repo.getGoodsDetail(goodsId, getGoodsDetailSuccess, getGoodsDetailFailed)
    }

    fun addToCart(goodsId: Int, count: Int) {
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_GOODS_ID] = goodsId
        map[Const.PARAM_COUNT] = count
        val json = Gson().toJson(map)
        val body = RequestBody.create(MediaType.parse(Const.MEDIA_PARSE), json)
        repo.addToCart(body, addToCartSuccess, addToCartFailed)
    }
}